<?php

namespace app\common;

class MixDataHandle
{
    //文件模型
    protected $speech;
    //角色分离模型
    protected $separate;

    public function __construct($speech, $separate)
    {
        $this->speech = $speech;
        $this->separate = $separate;
    }

    public function start()
    {
        //首先预处理角色分离信息
        $role_arr = $this->roleFileFormat($this->separate->separate);
        //合并角色分离信息和转写结果
        $speech_transfer = json_decode($this->speech->editor?:$this->speech->transfer, true)['list'];
        $mix_transfer_arr = $this->roleSeparationHandle($speech_transfer, $role_arr);

        return $mix_transfer_arr;
    }

    //处理角色分离数组
    private function roleFileFormat($file)
    {
        $role_arr = file($file);
        $foo = array();
        foreach ($role_arr as $role_sig) {
            $goo = trim(substr($role_sig, 1));
            $foo[] = [
                "startTime" => $this->time2Sec(trim(explode('==>', $goo)[0])),
                "endTime" => $this->time2Sec(trim(explode('==>', $goo)[1])),
                "sex" => 1,
                "character" => substr($role_sig, 0, 1),
            ];
        }
        //根据字段last_name对数组$data进行降序排列
        $start_times = $this->array_column($foo, 'startTime');
        array_multisort($start_times, SORT_ASC, $foo);
        return $foo;
    }

    protected function array_column($array, $column_name)
    {
        return array_map(function ($element) use ($column_name) {
            return $element[$column_name];
        }, $array);
    }

    public function time2Sec($time)
    {
        $time_arr = explode(':', $time);
        switch (count($time_arr)) {
            case 2:
                $sec = $time_arr[0] * 60 + $time_arr[1];
                break;
            case 3:
                $sec = $time_arr[0] * 60 * 60 + $time_arr[1] * 60 + $time_arr[2];
                break;
            default:
                $sec = 0;
                break;
        }
        return $sec;
    }

    //添加角色分离
    private function roleSeparationHandle($transform_data, $cha_data)
    {
        $i = $j = 0;
        $roleCostTimeforTrans = array();
        while ($j < count($transform_data) && $i < count($cha_data)) {
            $tran_stime = $transform_data[$j]['startTime'] + 0;
            $tran_etime = $transform_data[$j]['endTime'] + 0;
            $cha_stime = $cha_data[$i]['startTime'] + 0;
            $cha_etime = $cha_data[$i]['endTime'] + 0;
            $character = $cha_data[$i]['character'];
            if ($cha_etime <= $tran_etime) {
                if ($tran_stime <= $cha_stime) {
                    isset($roleCostTimeforTrans[$j][$character]) ? $roleCostTimeforTrans[$j][$character] += $cha_etime - $cha_stime : $roleCostTimeforTrans[$j][$character] = $cha_etime - $cha_stime;
                } else {
                    $roleCostTimeforTrans[$j][$character] = $cha_etime - $tran_stime;
                }
                if ($cha_etime == $tran_etime) {
                    $j++;
                }
                $i++;
            } else if ($cha_etime > $tran_etime) {
                if ($cha_stime <= $tran_stime) {
                    $roleCostTimeforTrans[$j][$character] = $tran_etime - $tran_stime;
                } else {
                    isset($roleCostTimeforTrans[$j][$character]) ? $roleCostTimeforTrans[$j][$character] += $tran_etime - $cha_stime : $roleCostTimeforTrans[$j][$character] = $tran_etime - $cha_stime;
                }
                $j++;
            }
        }
        $return = $this->insertSpeakertoTransform($transform_data, $roleCostTimeforTrans);
        return  $return;
    }

    private function insertSpeakertoTransform($transform_data, $speaker_data)
    {
        for ($i = 0; $i < count($speaker_data); $i++) {
            $speaker = $this->compareArrayItems($speaker_data[$i]);
            $transform_data[$i]['speaker'] = $speaker;
            $transform_data[$i]['sc'] = $i;
        }
        return $transform_data;
    }

    private function compareArrayItems($array)
    {
        $max = 0;
        $speaker = '';
        foreach ($array as $key => $value) {
            if ($value > $max) {
                $max = $value;
                $speaker = $key;
            }
        }
        return $speaker;
    }
}
